package com.ruoyi.warn.service.impl;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.UserBusinessSubType;
import com.ruoyi.event.mapper.EventBfRecordMapper;
import com.ruoyi.event.mapper.EventJfMapper;
import com.ruoyi.event.mapper.EventThMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.user.domain.UserInfo;
import com.ruoyi.user.mapper.UserInfoMapper;
import com.ruoyi.warn.domain.vo.AssessNumsVo;
import com.ruoyi.warn.domain.vo.RyDetailVo;
import com.ruoyi.warn.domain.vo.UnitKpiVo;
import com.ruoyi.warn.mapper.AssessMapper;
import com.ruoyi.warn.service.IAssessService;
import com.ruoyi.warn.service.IWarnService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.compress.utils.Lists;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * 智能预警Service业务层处理
 *
 * @author xiaohao
 * @date 2023-02-10
 */
@RequiredArgsConstructor
@Service
public class AssessServiceImpl implements IAssessService {

    private final UserInfoMapper userInfoMapper;
    private final SysUserMapper sysUserMapper;
    private final IWarnService warnService;
    private final EventThMapper thMapper;
    private final EventJfMapper jfMapper;
    private final EventBfRecordMapper bfRecordMapper;
    private final AssessMapper assessMapper;

    @Override
    public AssessNumsVo nums() {
        AssessNumsVo result = new AssessNumsVo();
        List<String> drugTypeList = Lists.newArrayList();
        drugTypeList.addAll(UserBusinessSubType.DRUG_SJSK.getDrugTypeList());
        drugTypeList.addAll(UserBusinessSubType.DRUG_ZDGZ.getDrugTypeList());
        drugTypeList.addAll(UserBusinessSubType.DRUG_CHDJ.getDrugTypeList());
        drugTypeList.addAll(UserBusinessSubType.DRUG_QJ.getDrugTypeList());
        drugTypeList.addAll(UserBusinessSubType.DRUG_KFJZ.getDrugTypeList());
        drugTypeList.addAll(UserBusinessSubType.DRUG_SLG.getDrugTypeList());

        // 社戒社康总人数
        result.setSjszrs(
            Convert.toInt(
                userInfoMapper.selectCount(
                    Wrappers.<UserInfo>lambdaQuery()
                        .in(UserInfo::getType, UserBusinessSubType.DRUG_SJSK.getDrugTypeList())
                )
            )
        );
        // 本月已签到人数
        result.setYqdrs(userInfoMapper.getByyqdrs());
        // 本月应签到人数
        result.setSqdrs(
            Convert.toInt(
                userInfoMapper.selectCount(
                    Wrappers.<UserInfo>lambdaQuery()
                        .in(UserInfo::getType, drugTypeList)
                )
            )
        );
        // 已尿检人数
        result.setYnjrs(warnService.getBqynjList().size());
        // 应尿检人数
        result.setSnjrs(
            Convert.toInt(
                userInfoMapper.selectCount(
                    Wrappers.<UserInfo>lambdaQuery()
                        .in(UserInfo::getType, drugTypeList)
                )
            )
        );

        // 谈话次数
        result.setThcs(thMapper.selectList().size());
        // 家访次数
        result.setJfcs(jfMapper.selectList().size());
        // 帮扶次数
        result.setBfcs(bfRecordMapper.selectList().size());

        // app安装人数
        result.setAppazzrs(
            Convert.toInt(
                sysUserMapper.selectCount(
                    Wrappers.<SysUser>lambdaQuery()
                        .isNotNull(SysUser::getOpenId)
                )
            )
        );
        // 未知统计
        result.setJyazrs(0); // 就业安置人数

        return result;
    }
    /**
     * 获取某单位各项指标率
     */
    @Override
    public UnitKpiVo getUnitKpiVo(Long deptId, String start, String end) {
        UnitKpiVo result = assessMapper.getUnitKpi(deptId, start, end);
        // 尿检率
        List<UserInfo> drugUsers = userInfoMapper.selectList(
            Wrappers.lambdaQuery(UserInfo.class)
                .in(UserInfo::getType, UserBusinessSubType.DRUG_SJSK.getDrugTypeList())
        );
        List<RyDetailVo> bqwnjUsers = warnService.getBqwnjListByUsers(drugUsers);
        int snjrs = drugUsers.size(); // 应尿检人数
        int wnjrs = bqwnjUsers.size(); // 未尿检人数
        if (result != null) result.setNjl(BigDecimal.valueOf(wnjrs/snjrs*100).setScale(2, BigDecimal.ROUND_HALF_UP));
        // xh_todo: 就业安置率
        return result;
    }

    /**
     * 计算某单位综合分值
     * 15*执行率+15*管控率+10*签到率+10*尿检率+10*谈话率+10*家访率+5*就业安置率+5*帮扶率+10*小程序安装率
     */
    @Override
    public BigDecimal getScore(UnitKpiVo kpi) {
        if (kpi != null) {
            BigDecimal score = BigDecimal.ZERO;
            score = score.add(kpi.getZxl().multiply(BigDecimal.valueOf(15)))
                .add(kpi.getGkl().multiply(BigDecimal.valueOf(15)))
                .add(kpi.getQdl().multiply(BigDecimal.valueOf(10)))
                .add(kpi.getNjl().multiply(BigDecimal.valueOf(10)))
                .add(kpi.getThl().multiply(BigDecimal.valueOf(10)))
                .add(kpi.getJfl().multiply(BigDecimal.valueOf(10)))
                .add(kpi.getJyazl().multiply(BigDecimal.valueOf(5)))
                .add(kpi.getBfl().multiply(BigDecimal.valueOf(5)))
                .add(kpi.getXcxazl().multiply(BigDecimal.valueOf(10)))
            ;
            return score.setScale(2, BigDecimal.ROUND_HALF_UP);
        }
        return BigDecimal.ZERO;
    }
}
